<?php

namespace App\Api\Controllers;

use App\Api\Foundation\Controllers\Controller;
use App\Models\Plate;
use App\Models\UserFollowPlate;
use Exception;
use GuzzleHttp\Client;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PlateController extends Controller
{
    /*
    *创建版块
    */
	public function add(Request $request)
    {
        $name = $request->input('name', '');
        $creator = $request->input('creator', '');
        $avatar = $request->input('avatar', '');
        $background = $request->input('background', '');
        $province_id = $request->input('province_id', '');
        $city_id = $request->input('city_id', '');
        $area_id = $request->input('area_id', '');
        $created_by = $request->input('created_by', '');
        if (!$name) {
            return $this->failed('版块名称不能为空');
        }
        if (!$creator) {
            return $this->failed('创建人不能为空');
        }
        if (!$avatar) {
            return $this->failed('版块头像不难为空');
        }
        if (!$province_id) {
            return $this->failed('请选择省份');
        }
        $data = [
            'name' => $name,
            'creator' => $creator,
            'avatar' => $avatar,
            'background' => $background,
            'province_id' => $province_id,
            'city_id' => $city_id,
            'area_id' => $area_id,
            'created_on' => time(),
            'created_by' => $created_by
        ];
        $res = Plate::insertGetId($data);
        if (!$res) {
            return $this->failed('创建失败');
        }
    	return $this->succeed($res);
    }

    /*
    *编辑版块
    */
    public function edit(Request $request)
    {
        $id = $request->input('id', '');
        $name = $request->input('name', '');
        $creator = $request->input('creator', '');
        $avatar = $request->input('avatar', '');
        $background = $request->input('background', '');
        $province_id = $request->input('province_id', '');
        $city_id = $request->input('city_id', '');
        $area_id = $request->input('area_id', '');
        $created_by = $request->input('created_by', '');
        $modified_by = $request->input('modified_by', '');

        if (!$id) {
            return $this->failed('缺失重要参数版块id');
        }
        if (!$name) {
            return $this->failed('版块名称不能为空');
        }
        if (!$creator) {
            return $this->failed('创建人不能为空');
        }
        if (!$avatar) {
            return $this->failed('版块头像不难为空');
        }
        if (!$province_id) {
            return $this->failed('请选择省份');
        }
        $data = [
            'name' => $name,
            'creator' => $creator,
            'avatar' => $avatar,
            'background' => $background,
            'province_id' => $province_id,
            'city_id' => $city_id,
            'area_id' => $area_id,
            'created_by' => $created_by,
            'modified_on' => time(),
            'modified_by' => $modified_by
        ];
        $res = Plate::where('id', $id)->update($data);
        if (!$res) {
            return $this->failed('编辑失败');
        }
        return $this->succeed($res);
    }

    /*
    *删除版块
    */
    public function del(Request $request)
    {
        $id = $request->input('id', '');
        if (!$id) {
            return $this->failed('缺失重要参数版块id');
        }
        $data = [
            'is_del' => 2,
            'deleted_on' => time()
        ];
        $res = Plate::where('id', $id)->update($data);
        if (!$res) {
            return $this->failed('删除失败');
        }
        return $this->succeed($res);
    }

    /*
    *关注版块
    */
    public function follow(Request $request)
    {
        $user_id = $request->input('user_id', '');
        $plate_id = $request->input('plate_id', '');
        $is_del = 1;
        if (!$user_id) {
            return $this->failed('请先登录');
        }
        if (!$plate_id) {
            return $this->failed('缺失重要参数版块id');
        }
        $info = UserFollowPlate::where('user_id', $user_id)->where('plate_id', $plate_id)->first();
        $info = $info ? $info->toArray() : [];
        if ($info) {
            if ($info['is_del'] == 1) {
                $is_del = 2;
            }
            $res = UserFollowPlate::where('user_id', $user_id)->where('plate_id', $plate_id)->update(['is_del' => $is_del, 'modified_by' => $user_id, 'modified_on' => time()]);
        } else {
            $res = UserFollowPlate::insertGetId([
                'user_id' => $user_id,
                'plate_id' => $plate_id,
                'is_del' => $is_del,
                'created_by' => $user_id,
                'created_on' => time()
            ]);
        }
        if (!$res) {
            return $this->failed('操作失败');
        }
        return $this->succeed($res);
    }

    /*
    *版块列表
    */
    public function list(Request $request)
    {
        $type = $request->input('type', 0);//0:综合、1:视频、2:图文、3:二手车列表
    }

}